jQuery(function ($) {
    var user;
    var uuid = jq.cookie("login");

    //结算页面遍历
    function listcar() {
        $.ajax({
            url: "/user/getcar",
            data: {
                "uuid": uuid,
            },
            success: function (data) {
                user = data.user;
                //购物车信息

                if (user != null) {
                    var $allprice = 0;
                    var $allcount = 0;
                    var sum = data.listcar.length;
                    if (sum>0 ) {
                        $(".car_tab").empty();
                        $.each(data.listcar, function (index, product) {
                            var $price = product.count * product.price;
                            $allcount = $allcount + product.count;
                            $allprice = $allprice + $price
                            var $th = $('<tr>\n' +
                                '            <td class="car_th" width="490">商品名称</td>\n' +
                                '            <td class="car_th" width="150">购买数量</td>\n' +
                                '            <td class="car_th" width="130">单价</td>\n' +
                                '            <td class="car_th" width="130">小计</td>\n' +
                                '            <td class="car_th" width="150">库存</td>\n' +
                                '          </tr>');
                            $(".car_tab").append($th);
                            var $tr = $('<tr><td><div class="c_s_img"><img src="files/' + product.filename + '" width="73" height="73" /></div>' + product.name + '</td>' +
                                '<td align="center">'+product.count+'</td>' +
                                '<td align="center" style="color:#ff4e00;">￥' + product.price + '</td>' + '<td align="center" style="color:#ff4e00;">￥' + $price + '</td>' + '<td align="center">'+product.stock+'</td></tr>');
                            $(".car_tab").append($tr);
                        })
                        var $trj = $('<tr height="70"> <td colspan="6"  border-bottom:0;"><label class="r_rad"><input type="checkbox" name="clear" /></label><label class="r_txt">清空购物车</label>' +
                            '<span class="fr">商品总价：<b style="font-size:22px; color:#ff4e00;">￥' + $allprice + '</b></span></td></tr><tr valign="top" height="150"><td colspan="6" align="right"><a href="index.html"><img src="images/buy1.gif" /></a>&nbsp; &nbsp; <a href="BuyCar_Two.html"><img src="images/buy2.gif" /></a>' +
                            '</td></tr>');
                        $('.car_tab').append($trj)
                    }else {
                        $('.car_tab').append($("<h2>您的购物车为空，请去添加商品吧！</h2>"));
                    }
                    $(".car_t>span").html(sum);
                }
            }
        })
    }

    listcar();

    //正则判断
    $(".content").on("change", ".car_ipt", function () {
        var $val = $(this).val();
        var $id = $(this).data("id");
        var regexp = /^[0-9]*[1-9][0-9]*$/;
        if (!regexp.test($val)) {
            $(this).val(1);
        }
        addcarto($id, user.loginname, $val, "buycar");
    })

    //加入购物车
    function addcarto(id, loginname, count, buycarto) {
        var uuid = $.cookie("login")
        $.ajax({
            url: "/user/addcar",
            data: {
                "id": id,
                "loginname": user.loginname,
                "count": count,
                "buycarto": buycarto,
            },
            success: function (data) {
                listcar();
                buycar();
            }
        })
    }
    //左上购物车遍历
    function buycar() {
        $.ajax({
            url: "/user/getcar",
            data: {
                "uuid": uuid,
            },
            success: function (data) {
                user = data.user;
                //购物车信息
                $(".car_bg").empty();
                if (user != null) {
                    var $allprice = 0;
                    var $allcount = 0;
                    var length = data.listcar.length;
                    if (length > 0) {
                        $.each(data.listcar, function (index, product) {
                            var $price = product.count * product.price;
                            $allcount = $allcount + product.count;
                            $allprice = $allprice + $price
                            var $ul = $('<ul class="cars"><li>\n' +
                                '<div class="img"><a href="#"><img src="files/' + product.filename + '" width="58"\n' + 'height="58"/></a></div>\n' +
                                '<div class="name"><a href="/Product.html?id=' + product.id + '">' + product.name + '</a></div>\n' +
                                '<div class="price"><font color="#ff4e00">￥' + product.price + '</font> X<span class="count">' + product.count + '</span></div>\n' + '</li></ul>')
                            $(".car_bg").append($ul);
                        })
                        var $jiesuan = $('<div class="price_sum">共计&nbsp; <font color="#ff4e00">￥</font><span>' + $allprice + '</span>\n' +
                            '</div>\n' + '<div class="price_a"><a href="BuyCar.html">去购物车结算</a></div>');
                        $('.car_bg').append($jiesuan)
                    } else {
                        $('.car_bg').append($('<div className="un_login"><h2 style="">您的购物车是空的!</h2></div>'));
                    }
                    $(".car_t>span").html(length);
                } else {
                    $(".car_bg").append('<div class="un_login">还未登录！<a href="Login.html" style="color:#ff4e00;">马上登录</a> 查看购物车！</div>');
                    $(".car_t>span").html(0);
                }
            }
        })
    }

    buycar();

    //货物数量加减
    $("body").on("click", ".car_btn_2", function () {
        var c = $(this).parent().find(".car_ipt").val();
        var $id = $(this).parent().find(".car_ipt").data("id");
        c = parseInt(c) + 1;
        $(this).parent().find(".car_ipt").val(c);
        addcarto($id, user.loginname, c, "buycar");
    })
    $("body").on("click", ".car_btn_1", function () {
        var c = $(this).parent().find(".car_ipt").val();
        var $id = $(this).parent().find(".car_ipt").data("id");
        if (c == 1) {
            c = 1;
        } else {
            c = parseInt(c) - 1;
            $(this).parent().find(".car_ipt").val(c);
        }
        addcarto($id, user.loginname, c, "buycar");
    })

    //点击删除
    $("body").on("click", ".del", function () {
        if (confirm("确定删除？")) {
            var id = $(this).data("id");
            del(id);
        }
    })
    //清空购物车
    $("body").on("change","input[name='clear']",function(){
        if(confirm("确定清空购物车么？")){
            del();
        }
    })
    //购物车删除方法
    function del(id) {
        $.ajax({
            url: "user/delcar",
            data: {
                "loginname": user.loginname,
                "id": id,
            },
            success: function (data) {
                if (data) {
                    alert("删除成功！");
                    listcar();
                    buycar();
                } else {
                    alert("删除失败！");
                }
            }
        })
    }

    function addcar(id, loginname, count) {
        var uuid = $.cookie("login")

        $.ajax({
            url: "/user/addcar",
            data: {
                "id": id,
                "loginname": user.loginname,
                "count": count,
            },
            success: function (data) {
                if (data) {
                    alert("成功加入购物车！");
                    buycar();
                } else {
                    alert("购物车已满10个！请先去结算！")
                }

            }
        })
    }

    //加入购物车
    $("body").on("click", ".j_car", function () {
        var id = $(this).data("id");
        var loginname = user.loginname;
        addcar(id, loginname);
    })



    //遍历收藏夹
    function shoucang(){
        $.ajax({
            url:"user/getcar",
            data:{
                "uuid":uuid,
            },
            success:function (data){
                $(".l_history").empty();
                var $tou=$('<div class="his_t">\n' + '<span class="fl">收藏夹</span>\n' +
                    '<span class="fr"><a href="javascript:void(0)">清空</a></span>\n' + '</div>');
                var $ul=$("<ul></ul>");
                $(".l_history").append($tou).append($ul);
                if(data.user !=null){
                    if(data.listsc.length>0){
                        $.each(data.listsc,function (index,product){
                            var $li = $('<li>\n' +
                                '<div class="img"><a href="#"><img src="files/'+product.filename+'" width="185" height="162"/></a></div>\n' +
                                '<div class="name"><a href="Product.html?id='+product.id+'">'+product.name+'</a></div>\n' +
                                '<div class="price">\n' +
                                '<font>￥<span>'+product.price+'</span></font> &nbsp; 18R\n' +
                                '</div>\n' + '</li>')
                            $ul.append($li);
                        })
                    }else {
                        var $li=$("<li><div>您的收藏夹为空。</div></li>")
                        $ul.append($li);
                    }
                }else {
                    var $li=$("<li><div>您还没有登陆，<a href='Login.html'>马上登陆</a>查看收藏夹。</div></li>")
                    $ul.append($li);
                }

            }
        })
    }
    shoucang();

    //加入收藏夹
    $('.cate_list').on('click',".ss",function(){
        var id = $(this).data("id");
        addshoucang(id);
    })
    function addshoucang(id){
        $.ajax({
            url:"/user/addshoucang",
            data:{
                "id":id,
                "loginname":user.loginname,
            },
            success:function (data){
                alert(data);
                shoucang();
            }
        })
    }
    //清空收藏夹
    $('.l_history').on("click","a",function (){
        clearshoucang();
    })

    function clearshoucang(){
        $.ajax({
            url:"/user/delshoucang",
            data:{
                "loginname":user.loginname,
            },
            success:function (data){
                if(data){
                    alert("清空成功！");
                    shoucang();
                }
            }
        })
    }

})